var longitude = "", latitude = "";

Page({
    data: {
        address: null,
        offline: 1,
        payment: -1,
        show_payment: !1,
        //抽奖记录id 未中奖logid==0
        logid: 0,
        jx:'0',
        dh:'0',
        aid:0,
        is_distributor:''
    },
    onLoad: function(t) {
        getApp().page.onLoad(this, t);
        console.log('aid==',t)
        var e = t.goods_info, a = JSON.parse(e);
        a && this.setData({
            goods_info: a,
            jx:t.jx,
            dh:t.dh,
            aid:t.aid,
            lgs_id:t.lgs_id,
            offline: 1,
            id: a.goods_id,
            log_id: t.log_id ? t.log_id: 0
        });
        wx.setNavigationBarTitle({
          title: '确认订单',
        })
    },
    onReady: function() {},
    onShow: function() {
        getApp().page.onShow(this), getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        });
        var n = this, t = getApp().core.getStorageSync(getApp().const.PICKER_ADDRESS);
        t && (n.setData({
            address: t,
            name: t.name,
            mobile: t.mobile
        }), getApp().core.removeStorageSync(getApp().const.PICKER_ADDRESS));
        var e = "";
        if (n.data.address && n.data.address.id) e = n.data.address.id;
        console.log('jx',n.data)
        if(n.data.jx == '1'){
            var per = getApp().api.surprise.submit_preview
            var data = {
                goods_info: JSON.stringify(n.data.goods_info),
                address_id: e,
                logid: n.data.log_id,
                aid:n.data.aid
              }
        }else if (n.data.dh == '1'){
            var per = getApp().api.surprise.exchange_submit_preview
            var data = {
                goods_info: JSON.stringify(n.data.goods_info),
                address_id: e,
                logid: n.data.log_id,
                aid:n.data.aid,
                lgs_id:n.data.lgs_id
              }
        }else{
            var per = getApp().api.integral.submit_preview
            var data = {
                goods_info: JSON.stringify(n.data.goods_info),
                address_id: e,
                logid: n.data.log_id
              }
        }
        getApp().request({
            url:per,
            data:data,
            success: function(t) {
                if (getApp().core.hideLoading(), 0 == t.code) {
                    var e = t.data.shop_list, a = {};
                    e && 1 == e.length && (a = e[0]), t.data.is_shop && (a = t.data.is_shop);
                    var o = t.data.total_price;
                    if (t.data.express_price) s = t.data.express_price; else var s = 0;
                    var i = t.data.goods;
                    if(n.data.dh == '1'){
                        n.setData({
                            attr_integral:  i.exchange_attr[0].exchange_integral,
                        })
                    }
                    n.setData({
                        goods: i,
                        address: t.data.address,
                        express_price: s,
                        shop_list: t.data.shop_list,
                        shop: a,
                        name: t.data.address ? t.data.address.name : "",
                        mobile: t.data.address ? t.data.address.mobile : "",
                        total_price: parseFloat(o).toFixed(2),
                        send_type: t.data.send_type,
                        attr: i.attr,
                        attr_price: i.attr_price,
                       
                        is_distributor:t.data.is_distributor
                    }), 1 == t.data.send_type && n.setData({
                        offline: 1
                    }), 2 == t.data.send_type && n.setData({
                        offline: 2
                    }), n.getTotalPrice();
                } else getApp().core.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1,
                    confirmText: "返回",
                    success: function(t) {
                        t.confirm && getApp().core.navigateBack({
                            delta: 1
                        });
                    }
                });
            }
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    getOffline: function(t) {
        var e = this, a = (e.data.express_price, e.data.total_price);
        1 == t.currentTarget.dataset.index ? e.setData({
            offline: 1,
            total_price: a
        }) : e.setData({
            offline: 2
        }), e.getTotalPrice();
    },
    showShop: function(t) {
        var e = this;
        e.dingwei(), e.data.shop_list && 1 <= e.data.shop_list.length && e.setData({
            show_shop: !0
        });
    },
    dingwei: function() {
        var e = this;
        getApp().core.chooseLocation({
            success: function(t) {
                longitude = t.longitude, latitude = t.latitude, e.setData({
                    location: t.address
                });
            },
            fail: function(t) {
                getApp().getauth({
                    content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
                    author: "scope.userLocation",
                    success: function(t) {
                        t && (t.authSetting["scope.userLocation"] ? e.dingwei() : getApp().core.showToast({
                            title: "您取消了授权",
                            image: "/images/icon-warning.png"
                        }));
                    }
                });
            }
        });
    },
    pickShop: function(t) {
        var e = this, a = t.currentTarget.dataset.index;
        "-1" == a || -1 == a ? e.setData({
            shop: !1,
            show_shop: !1
        }) : e.setData({
            shop: e.data.shop_list[a],
            show_shop: !1
        });
    },
    bindkeyinput: function(t) {
        this.setData({
            content: t.detail.value
        });
    },
    KeyName: function(t) {
        this.setData({
            name: t.detail.value
        });
    },
    KeyMobile: function(t) {
        this.setData({
            mobile: t.detail.value
        });
    },
    orderSubmit: function(t) {
        var e = this, a = e.data.offline, o = {};

        if (1 == a) {
            if (!e.data.address || !e.data.address.id) return void getApp().core.showToast({
                title: "请选择收货地址",
                image: "/images/icon-warning.png"
            });
            if (o.address_id = e.data.address.id, e.data.total_price) {
                if (0 < e.data.total_price) var s = 2; else s = 1;
                o.type = s;
            }
        } else {
            if (o.address_name = e.data.name, o.address_mobile = e.data.mobile, !e.data.shop.id) return void getApp().core.showModal({
                title: "警告",
                content: "请选择门店",
                showCancel: !1
            });
            if (o.shop_id = e.data.shop.id, !o.address_name || null == o.address_name) return void e.showToast({
                title: "请填写收货人",
                image: "/images/icon-warning.png"
            });
            if (!o.address_mobile || null == o.address_mobile) return void e.showToast({
                title: "请填写联系方式",
                image: "/images/icon-warning.png"
            });
        }
        if (o.offline = a, e.data.content && (o.content = e.data.content), e.data.goods_info) {
            var i = e.data.attr, n = [];
            for (var r in i) {
                var d = {
                    attr_id: i[r].attr_id,
                    attr_name: i[r].attr_name
                };
                n.push(d);
            }
            console.log('e.data.goods_info',e.data.goods_info)
            if(e.data.goods_info.attr_integral && e.data.is_distributor==0){
                delete e.data.goods_info.attr_integral
            }
            console.log('e.data.goods_info111112222',e.data.goods_info)
            e.data.goods_info.attr = n, o.goods_info = JSON.stringify(e.data.goods_info);
        }
        e.data.express_price && (o.express_price = e.data.express_price), o.attr = JSON.stringify(i), 
        o.logid = e.data.log_id,
        getApp().core.showLoading({
            title: "提交中",
            mask: !0
        }), 
        o.formId = t.detail.formId;
        if(e.data.jx == '1'){
            var sub = getApp().api.surprise.submit
            o.aid = e.data.aid
        }else if(e.data.dh == '1'){
            var sub = getApp().api.surprise.exchange_submit
            o.aid = e.data.aid,
            o.lgs_id = e.data.lgs_id
        }else{
            var sub = getApp().api.integral.submit
        }
        getApp().request({
            url:sub,
            method: "post",
            data: o,
            success: function(t) {
                if(e.data.jx == '1'){
                    var order = '/pages/integral-mall/order/order?status=1' + '&integral_mall=1'
                }else if(e.data.dh == '1'){
                    var order = '/pages/integral-mall/order/order?status=1' + '&integral_mall=2'
                }else{
                    var order = '/pages/order/order?status=1' 
                }
                getApp().core.hideLoading(), 0 == t.code ? 1 == t.type ?getApp().core.showToast({
                    title: '兑换成功',
                    icon: 'success',
                    duration: 2000,
                    success: function(t) {
                        setTimeout(function () {
                            getApp().core.redirectTo({
                                url :order
                            })
                           }, 1000)
                    }
                  }): getApp().core.requestPayment({
                    _res: t,
                    timeStamp: t.data.timeStamp,
                    nonceStr: t.data.nonceStr,
                    package: t.data.package,
                    signType: t.data.signType,
                    paySign: t.data.paySign,
                    success: function(e) {
                        wx.setStorageSync('address_id', o.address_id)
                        wx.setStorageSync('adds', '2')
                    },
                    complete: function(t) {
                        if(e.data.jx == '1'){
                            var order = '/pages/integral-mall/order/order?status=1'
                        }else{
                            // var order = '/pages/order/order?status=1'
                            var order = '/pages/integral-mall/index/index'
                        }
                        "requestPayment:fail" != t.errMsg && "requestPayment:fail cancel" != t.errMsg ? "requestPayment:ok" == t.errMsg &&getApp().core.requestSubscribeMessage({
                            tmplIds: ['_XGsVLLisG8_LTbDnBk_4yIr2GbwvkNi8aSsKiUN_80'],
                            success(res) {
                                console.log('订阅消息', res)
                                if(res == "requestSubscribeMessage:ok"){
                                    getApp().core.redirectTo({
                                        url: order
                                    })
                                }else{
                                    getApp().core.redirectTo({
                                        url: order
                                    })
                                }
                            }
                        }) : getApp().core.showModal({
                            title: "提示",
                            content: "订单尚未支付",
                            showCancel: !1,
                            confirmText: "确认",
                            success: function(t) {
                                if(e.data.jx == '1'){
                                    var order = '/pages/integral-mall/order/order?status=0&integral_mall=1'
                                }else if(e.data.dh == '1'){
                                    var order = '/pages/integral-mall/order/order?status=0&integral_mall=2'
                                }
                                else{
                                    var order = '/pages/order/order?status=0'
                                }
                                t.confirm && getApp().core.redirectTo({
                                    url:order
                                });
                            }
                        });
                    }
                }) : getApp().core.showToast({
                    title: t.msg,
                    image: "/images/icon-warning.png"
                });
            }
        });
    },
    getTotalPrice: function() {
        var t = parseFloat(this.data.total_price), e = this.data.offline, a = this.data.express_price, o = 0;
        o = 2 == e ? t : t + a, this.setData({
            new_total_price: parseFloat(o).toFixed(2)
        });
    }
});